//
//  HJResetPasswordController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/11/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJResetPasswordController.h"
#import "HJToResetViewController.h"

@interface HJResetPasswordController ()
@property (nonatomic, assign) NSInteger timeout;
@property (weak, nonatomic) IBOutlet UIButton *sendCode;
@property (weak, nonatomic) IBOutlet UILabel *phone;
@property (weak, nonatomic) IBOutlet UITextField *code;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btmY;
@property(nonatomic,strong)NSNumber * codeNo;
@end

@implementation HJResetPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"密码重置";
    self.nextBtn.layer.shadowOpacity = 0.2;
    self.nextBtn.layer.shadowRadius = 3;
    self.nextBtn.layer.shadowOffset = CGSizeZero;
    self.nextBtn.layer.shadowColor = [UIColor blackColor].CGColor;
    self.phone.text = self.mobile;
    
    [[self.code.rac_textSignal filter:^BOOL(NSString*text){
        return text.length >=4;
    }] subscribeNext:^(NSString *text){
        self.code.text = [text substringToIndex:4];
    }];
}
- (IBAction)sendCode:(UIButton *)sender {
    [self countDown:sender];
    [self HTTPSmsCode];
}

- (IBAction)nextBtn:(UIButton *)sender {
    if (self.code.text.length<1) {
        [self showHint:@"请设置验证码"];
        return;
    }
    if ([self.codeNo isEqual:self.code.text.numberValue]) {
        HJToResetViewController * vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"password"];
        vc.code = self.code.text;
        vc.mobile  = self.mobile;
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        [self showErrorHUDWithError:@"验证码不正确"];
    }
}
- (void)HTTPSmsCode {
    [TZHttpTool post:SendSms params:@{ @"mobile" : self.mobile,@"type": @"0"} success:^(NSDictionary *result) {
        [self showHint:@"验证码发送成功"];
        self.codeNo = result[@"data"][@"code"];
        NSLog(@"%@",self.codeNo);
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
#pragma mark - Private Methods
- (void)countDown:(UIButton *)btn {
    _timeout = 60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if (_timeout <= 0) {
            // 倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                btn.backgroundColor = HJColor(93, 188, 255);
                btn.userInteractionEnabled = YES;
                [btn setTitleColor:HJColorRGB(255) forState:UIControlStateNormal];
                [btn setTitle:@"发送" forState:UIControlStateNormal];
            });
        } else {
            NSString *strTime = [NSString stringWithFormat:@"%.2ld秒", (long)_timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                [btn setBackgroundColor:HJColorRGB(250)];
                btn.userInteractionEnabled = NO;
                [btn setTitleColor:HJColor(165, 170, 180) forState:UIControlStateNormal];
                [btn setTitle:strTime forState:UIControlStateNormal];
            });
            _timeout--;
        }
    });
    dispatch_resume(_timer);
}
@end
